package com.digdes.calendar.gwt.client.widget;

import com.digdes.calendar.gwt.client.dto.EventDTOImpl;
import com.digdes.calendar.gwt.client.service.CalendarService;
import com.digdes.calendar.gwt.client.service.CalendarServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

import java.util.ArrayList;
import java.util.List;

public class DayWidget extends Composite implements ClickListener {
    private final CalendarServiceAsync calendarService = GWT
            .create(CalendarService.class);
    public Grid dayGrid = new Grid(24, 2);
    private NavBar navbar = new NavBar();
    private Data data = new Data();
    public DateUtil date = new DateUtil();
    public Grid titleGrid = new Grid(1, 2);
    public CellGridDay hourGrid;
    public List<CellGridDay> gridInCellList = new ArrayList<CellGridDay>();
    private List<EventDTOImpl> eventDTOList = new ArrayList<EventDTOImpl>();
    private AbsolutePanel aPanel = new AbsolutePanel();
    private List<EventLabel> eventLabels = new ArrayList<EventLabel>();
    private EventsPanel eventsPanel = new EventsPanel();
    private ScrollPanel scrollPanel;
    private ShowEventDBox eventDBox;

    public DayWidget() {
        DockPanel outer = new DockPanel();
        setWidget(outer);
        dayGrid.setHeight("550px");
        dayGrid.setWidth("980px");
        dayGrid.setCellPadding(0);
        dayGrid.setCellSpacing(0);
        aPanel.setSize("100%", "1200px");
        aPanel.add(dayGrid);
        scrollPanel = new ScrollPanel(aPanel);
        scrollPanel.setSize("1000px", "550px");
        scrollPanel.addStyleName("scrollDay");

        DockPanel dayHeader = new DockPanel();

        dayHeader.add(titleGrid, DockPanel.CENTER);
        dayHeader.setCellHorizontalAlignment(titleGrid,
                HasAlignment.ALIGN_CENTER);
        titleGrid.addStyleName("titleGrid");

        dayHeader.setStyleName("dayheader");

        outer.add(navbar, DockPanel.NORTH);
        outer.add(dayHeader, DockPanel.NORTH);
        outer.add(scrollPanel, DockPanel.CENTER);
        outer.setStyleName("CalendarWidget");


        drawDayCalendar();
    }

    public void setDataEventsPanel() {
        eventsPanel.setData(date.getDay(), date.getMonth(), date.getYear(), eventDTOList);
    }

    public void drawDayCalendar() {

        for (int i = 0; i < data.hours.length; i++) {
            hourGrid = new CellGridDay(2, 1);
            dayGrid.setText(i, 0, data.hours[i]);
            dayGrid.getCellFormatter().setStyleName(i, 0, "hours");
            dayGrid.getCellFormatter().addStyleName(i, 0, "hoursSecond");
            dayGrid.getCellFormatter().addStyleName(i, 0, "cellRightBorder");
            hourGrid.getCellFormatter().setStyleName(0, 0, "hoursFirst");
            hourGrid.getCellFormatter().setStyleName(1, 0, "hoursSecond");
            hourGrid.setWidth("100%");
            hourGrid.setHeight("50px");
            hourGrid.setCellPadding(0);
            hourGrid.setCellSpacing(0);
            hourGrid.setHour(i);
            dayGrid.setWidget(i, 1, hourGrid);
            gridInCellList.add(hourGrid);
        }
        getEvents();
    }

    public void getEvents() {
        clearDayWidget();
        String s = date.getDate().getDate() + "." + (date.getMonth() + 1) + "." + date.getYear();
        calendarService.getEvents(s, new AsyncCallback<ArrayList<EventDTOImpl>>() {
            public void onFailure(Throwable caught) {
            }

            public void onSuccess(ArrayList<EventDTOImpl> result) {
                eventDTOList.addAll(result);
                setEventInCellDay();
            }
        });
    }

    private void setTitleGridData() {
        titleGrid.setText(0, 0, date.getDay() + " " + data.otherMonths[date.getMonth()] + ", ");
        titleGrid.setText(0, 1, data.daysFull[date.getDate().getDay()]);
    }

    private void clearDayWidget() {
        eventDTOList.clear();
        scrollPanel.scrollToTop();
        for (EventLabel label : eventLabels) {
            label.removeFromParent();
            RootPanel.get().remove(label);
        }
    }

    int width = 927;
    int moveLeft = 255;
    int moveTop = 100;
    int left = 0;

    /* public void setEventInCellDay() {
        clearDayWidget();
        setTitleGridData();
        setDataEventsPanel();


        for (CellGridDay c : gridInCellList) {
            int countEvent = 0;
            left = 0;
            if (date.getDay() == date.getNowDay()) {
                c.addStyleName("today");
            } else c.removeStyleName("today");

            for (EventDTOImpl eC : eventDTOList) {
                if ((eC.getDateStart().getDate() == date.getDay()) &&
                        (eC.getDateStart().getMonth() == date.getMonth()))
                    if (eC.getDateStart().getDate() == eC.getDateFinish().getDate()) {
                        if (eC.getDateStart().getHours() == c.getHour()) {
                            countEvent++;
                        }
                    }
            }
            for (EventDTOImpl e : eventDTOList) {
                if (e.getDateStart().getDate() == e.getDateFinish().getDate()) {
                    if (e.getDateStart().getHours() == c.getHour()) {
                        EventLabel panel = new EventLabel(e);
                        String title = e.getNameEvent() + "\n" + e.getDateStart().getHours() + ":" +
                                date.addZeroInMinutes(e.getDateStart().getMinutes()) +
                                " - " + e.getDateFinish().getHours() + ":" +
                                date.addZeroInMinutes(e.getDateFinish().getMinutes());
                        panel.setStyleName("EventLabelDay");
                        float minutes = (float) ((e.getDateFinish().getMinutes() - e.getDateStart().getMinutes()) * 0.8);
                        int minutesStart = (int) (e.getDateStart().getMinutes() * 0.8);
                        panel.setHeight(String.valueOf((e.getDateFinish().getHours() -
                                e.getDateStart().getHours()) * 50 + minutes - 3));
                        panel.setWidth(String.valueOf((width / countEvent) - 7));
                        panel.setText(title);
                        panel.setTitle(title);
                        panel.addClickListener(this);
                        aPanel.add(panel, (c.getAbsoluteLeft() - moveLeft +
                                c.getLeft()),
                                minutesStart + c.getAbsoluteTop() - moveTop);

                        left += (width / countEvent);
                        c.setLeft(left);
                        eventLabels.add(panel);

                    }
                }
            }
            c.setLeft(0);
        }
    }*/

    public void setEventInCellDay() {
        setTitleGridData();
        setDataEventsPanel();
        for (CellGridDay c : gridInCellList) {
            left = 0;
            c.getEventDTOList().clear();
            if (date.getDay() == date.getNowDay()) {
                c.addStyleName("today");
            } else c.removeStyleName("today");

            int countEvent = 0;
            for (EventDTOImpl eventDTO : eventDTOList) {
                if (eventDTO.getDateStart().getDate() == eventDTO.getDateFinish().getDate()
                        && eventDTO.getDateStart().getDate() == date.getDay()
                        && eventDTO.getDateStart().getMonth() == date.getMonth()
                        && (eventDTO.getDateStart().getYear() + 1900) == date.getYear())
                {
                    if (eventDTO.getDateStart().getHours() == c.getHour()) {
                        countEvent++;
                        c.getEventDTOList().add(eventDTO);
                    }
                }
            }
            for (EventDTOImpl e : c.getEventDTOList()) {
                drawEvent(c, e, countEvent);
            }
            c.setLeft(0);
        }
    }

    private void drawEvent(CellGridDay c, EventDTOImpl e, int countEvent) {
        EventLabel panel = new EventLabel(e);
                        String title = e.getNameEvent() + "\n" + e.getDateStart().getHours() + ":" +
                                date.addZeroInMinutes(e.getDateStart().getMinutes()) +
                                " - " + e.getDateFinish().getHours() + ":" +
                                date.addZeroInMinutes(e.getDateFinish().getMinutes());
                        panel.setStyleName("EventLabelDay");
                        float minutes = (float) ((e.getDateFinish().getMinutes() - e.getDateStart().getMinutes()) * 0.8);
                        int minutesStart = (int) (e.getDateStart().getMinutes() * 0.8);
                        panel.setHeight(String.valueOf((e.getDateFinish().getHours() -
                                e.getDateStart().getHours()) * 50 + minutes - 3));
                        panel.setWidth(String.valueOf((width / countEvent) - 7));
                        panel.setText(title);
                        panel.setTitle(title);
                        panel.addClickListener(this);
                        aPanel.add(panel, (c.getAbsoluteLeft() - moveLeft +
                                c.getLeft()),
                                minutesStart + c.getAbsoluteTop() - moveTop);

                        left += (width / countEvent);
                        c.setLeft(left);
                        eventLabels.add(panel);
      /*  EventLabel label = new EventLabel(e);
        String title = e.getNameEvent() + "\n" + e.getDateStart().getHours() + ":" +
                date.addZeroInMinutes(e.getDateStart().getMinutes()) +
                " - " + e.getDateFinish().getHours() + ":" +
                date.addZeroInMinutes(e.getDateFinish().getMinutes());
        label.setStyleName("EventLabelDay");
        float minutes = (float) ((e.getDateFinish().getMinutes() - e.getDateStart().getMinutes()) * 0.8);
        int minutesStart = (int) (e.getDateStart().getMinutes() * 0.8);
        label.setHeight(String.valueOf((e.getDateFinish().getHours() -
                e.getDateStart().getHours()) * 50 + minutes - 3));
        label.setWidth(String.valueOf((Util.WIDTH_WEEK_CELL / countEvent) - 6));
        label.setText(title);
        label.setTitle(title);
        label.addClickListener(this);
        aPanel.add(label, c.getAbsoluteLeft() - moveLeft + c.getLeft(),
                minutesStart + c.getAbsoluteTop() - moveTop);
        left += (Util.WIDTH_WEEK_CELL / countEvent);
        c.setLeft(left);
        eventLabels.add(label);*/


    }

    public void onClick(Widget sender) {
        final EventLabel label = (EventLabel) sender;
        eventDBox = new ShowEventDBox(label.getEventDTO());
        eventDBox.deleteBtn.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                deleteEvent(label);
            }
        });
        eventDBox.center();
    }

    private void deleteEvent(final EventLabel label) {
        eventDBox.deleteEvent(label, eventLabels);
        for (CellGridDay c : gridInCellList) {
            c.getEventDTOList().remove(label.getEventDTO());
        }
    }
    public EventsPanel getEventsPanel() {
        return eventsPanel;
    }

    private class NavBar extends Composite {

        public DockPanel bar = new DockPanel();
        public HTML titleYear = new HTML();
        public Grid gridTitleYear = new Grid(1, 2);

        public NavBar() {

            setWidget(bar);
            bar.setStyleName("navbar");
            titleYear.setStyleName("header");
            data = new Data();

            bar.add(gridTitleYear, DockPanel.CENTER);
            bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
            bar.setCellHorizontalAlignment(gridTitleYear,
                    HasAlignment.ALIGN_CENTER);
            bar.setCellVerticalAlignment(gridTitleYear,
                    HasAlignment.ALIGN_MIDDLE);
            bar.setCellWidth(gridTitleYear, "100%");
        }
    }

}
